<?php
//首页控制器
class IndexControl extends CommonControl
{

	// 分类
	private $cid;
	// 地区
	private $lid;
    // 价格
    private $pid;

    private $model;


	/**
	 * [__auto 初始化]
	 * @return [type] [description]
	 */
	public function __auto()
	{
        // 分类
		$this->cid = Q('get.cid',0,'intval');
        // 地区
		$this->lid = Q('get.lid',0,'intval');
        // 价格
        $this->pid = Q('get.pid',0);

        $this->model =  K('GoodsView');

	}

	/**
	 * [index 首页]
	 * @return [type] [description]
	 */
    public function index()
    {
    	// 设置分类
    	$this->set_category();
     	// 设置地区
    	$this->set_city();
        // 设置价格
        $this->set_price();


        // 设置条件
        $where=$this->model->set_search_where();
        // 分页
        $count= $this->model->where($where)->count();
        $page = new Page($count,6);
        $this->assign('page',$page->show());
        // 数据
        $goods= $this->model->get_goods_all($where,20);
        $this->assign('goods',$goods);
       
     	$this->display();
    }



    /**
     * [set_category 设置分类筛选模板]
     */
    private function set_category()
    {
    	$url = __URL__;
    	$url =url_param_remove('cid');
    	$categoryModel = K('Category');

    	// 顶级分类
    	$category = $categoryModel->topCategory;
    	$tempArr = array();
    	// get中没有cid
    	if(!$this->cid)
    	{
    		$tempArr[] = "<a href='{$url}' class='active'>全部</a>";
    	}
    	else
    	{
    		$tempArr[] = "<a href='{$url}'>全部</a>";
    	}

    	// get中有cid
		$cate = $categoryModel->get_category_one($this->cid);
		$pid = $cate['pid']?$cate['pid']:$this->cid;

    	foreach($category as $v)
    	{
    		if($v['cid'] == $pid)
    		{
    			$tempArr[] = "<a href='{$url}/cid/{$v['cid']}' class='active'>{$v['cname']}</a>";
    		}
    		else
    		{
    			$tempArr[] = "<a href='{$url}/cid/{$v['cid']}'>{$v['cname']}</a>";
    		}
    	}
    	$this->assign('topCategory',$tempArr);



    	// 子集
		$tempArr = array();
		if($this->cid)
		{
			// 读取子集
			$sonCategory = $categoryModel->get_son_category($pid);
            if($sonCategory)
            {
                if($this->cid==$pid)
                {
                    $tempArr[] = "<a href='{$url}/cid/{$pid}' class='active'>全部</a>";
                }
                else
                {
                    $tempArr[] = "<a href='{$url}/cid/{$pid}'>全部</a>";
                }


                foreach($sonCategory as $v)
                {
                    if($this->cid==$v['cid'])
                    {
                        $tempArr[] = "<a href='{$url}/cid/{$v['cid']}' class='active'>{$v['cname']}</a>";
                    }
                    else
                    {
                        $tempArr[] = "<a href='{$url}/cid/{$v['cid']}'>{$v['cname']}</a>";
                    }
                }
            }
			
		}
		$this->assign('sonCategory',$tempArr);

    }



    /**
     * [set_city 设置地区筛选模板]
     */
    private function set_city()
    {


        $url = __URL__;
        $url =url_param_remove('lid');
        $cityModel = K('City');

        // 顶级分类
        $city = $cityModel->topCity;
        $tempArr = array();
        // get中没有cid
        if(!$this->lid)
        {
            $tempArr[] = "<a href='{$url}' class='active'>全部</a>";
        }
        else
        {
            $tempArr[] = "<a href='{$url}'>全部</a>";
        }

        // get中有cid
        $cityInfo = $cityModel->get_city_one($this->lid);
        $pid = $cityInfo['pid']?$cityInfo['pid']:$this->lid;

        foreach($city as $v)
        {
            if($v['lid'] == $pid)
            {
                $tempArr[] = "<a href='{$url}/lid/{$v['lid']}' class='active'>{$v['lname']}</a>";
            }
            else
            {
                $tempArr[] = "<a href='{$url}/lid/{$v['lid']}'>{$v['lname']}</a>";
            }
        }
        $this->assign('topLocality',$tempArr);


        // 子集
        $tempArr = array();
        if($this->lid)
        {
            // 读取子集
            $sonCity = $cityModel->get_son_city($pid);
            if($sonCity)
            {
                if($this->lid==$pid)
                {
                    $tempArr[] = "<a href='{$url}/lid/{$pid}' class='active'>全部</a>";
                }
                else
                {
                    $tempArr[] = "<a href='{$url}/lid/{$pid}'>全部</a>";
                }


                foreach($sonCity as $v)
                {
                    if($this->lid==$v['lid'])
                    {
                        $tempArr[] = "<a href='{$url}/lid/{$v['lid']}' class='active'>{$v['lname']}</a>";
                    }
                    else
                    {
                        $tempArr[] = "<a href='{$url}/lid/{$v['lid']}'>{$v['lname']}</a>";
                    }
                }
            }
            
        }
        $this->assign('sonLocality',$tempArr);
    }



    /**
     * [set_price 设置价格筛选模板]
     */
    public function set_price()
    {
        $url = __URL__;
        $url =url_param_remove('pid');

        if($this->cid)
        {
            $categoryModel = K('Category');
            // 父级cid
            $cate = $categoryModel->get_category_one($this->cid);
            $pid = $cate['pid']?$cate['pid']:$this->cid;

        }
        else
        {
             $pid='all';
        }
        
        $price = C('price');


        

        $tempArr = array();
        if(!$this->pid)
        {
           $tempArr[] = "<a href='{$url}' class='active'>全部</a>";
        }
        else
        {
            $tempArr[] = "<a href='{$url}'>全部</a>";
        }

        foreach($price[$pid] as $v)
        {
           
            if($this->pid==$v[1])
            {
                $tempArr[] = "<a href='{$url}/pid/{$v[1]}' class='active'>{$v[0]}</a>";
            }
            else
            {
                 $tempArr[] = "<a href='{$url}/pid/{$v[1]}'>{$v[0]}</a>";
            }
        }

       $this->assign('price',$tempArr);

    }



}
?>